<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="用户" prop="userId">
        <el-input
          v-model="queryParams.userId"
          placeholder="请输入需求方用户"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="会话标识" prop="sessionId">
        <el-input
          v-model="queryParams.sessionId"
          placeholder="请输入会话标识"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="主题" prop="topicId">
        <el-input
          v-model="queryParams.topicId"
          placeholder="请输入主题"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="商机标题" prop="title">
        <el-input
          v-model="queryParams.title"
          placeholder="请输入商机标题"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-table v-loading="loading" :data="requireList" @selection-change="handleSelectionChange">
      <el-table-column label="用户" align="center" prop="userName"/>
      <el-table-column label="会话标识" align="center" prop="sessionId"/>
      <el-table-column label="主题" align="center" prop="topicId"/>
      <el-table-column label="案例编号" align="center" prop="identify"/>
      <el-table-column label="商机标题" align="center" prop="title"/>
      <el-table-column label="商机内容" align="center" prop="content" :show-overflow-tooltip="true"/>
      <el-table-column label="标签" align="center" prop="tags" :show-overflow-tooltip="true"/>
      <el-table-column label="状态" align="center" prop="status" width="50">
        <template slot-scope="scope">
          <el-switch
            v-model="scope.row.status"
            active-value="0"
            inactive-value="1"
            @change="handleStatusChange(scope.row)"
          ></el-switch>
        </template>
      </el-table-column>>
      <el-table-column label="创建时间" align="center" prop="createTime">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
          <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-plus"
            @click="handleSummary(scope.row)"
            v-hasPermi="['system:require:edit']"
          >合作方案</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:require:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:require:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改用户需求信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="960px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="会话标识" prop="sessionId">
          <el-input v-model="form.sessionId" placeholder="请输入会话标识" readonly/>
        </el-form-item>
        <el-form-item label="主题" prop="topicId">
          <el-input v-model="form.topicId" placeholder="请输入主题" readonly/>
        </el-form-item>
        <el-form-item label="案例编号" prop="identify">
          <el-input v-model="form.identify" placeholder="请输入案例编号" />
        </el-form-item>
        <el-form-item label="商机标题" prop="title">
          <el-input v-model="form.title" placeholder="请输入商机标题" />
        </el-form-item>
        <el-form-item label="商机内容" prop="content">
          <!--<editor v-model="form.content" :min-height="192"/>-->
          <mavon-editor ref="md" prop="content" v-model="form.content" :subfield = "false"/>
        </el-form-item>
        <el-form-item label="标签" prop="tags">
          <el-input v-model="form.tags" placeholder="请输入标签" />
        </el-form-item>
        <el-form-item label="排序" prop="sort">
          <el-input v-model="form.sort" placeholder="请输入排序" type="number"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 添加需求总结方案信息对话框 -->
    <el-dialog :title="title" :visible.sync="openSummary" width="960px" append-to-body>
      <el-form ref="form" :model="form2" :rules="rules2" label-width="100px">
        <el-form-item label="商机标题" prop="requireId">
          <el-input v-model="form2.title" readonly/>
        </el-form-item>
        <el-form-item label="供方用户" prop="userId">
          <el-select v-model="form2.userId" placeholder="请选择供方用户">
            <el-option
              v-for="item in userOptions"
              :key="item.userId"
              :label="item.userName"
              :value="item.userId"
              :disabled="item.status == 1"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="推荐标签" prop="tag">
          <el-input v-model="form2.tag" placeholder="请输入推荐标签" />
        </el-form-item>
        <el-form-item label="方案主图" prop="mainImage">
          <image-upload v-model="form2.mainImage"/>
        </el-form-item>
        <el-form-item label="方案内容" prop="content">
          <!--<editor v-model="form2.content" :min-height="192"/>-->
          <mavon-editor ref="md" prop="content" v-model="form2.content" :subfield = "false"/>
        </el-form-item>
        <el-form-item label="方案标签" prop="tags">
          <el-input v-model="form2.tags" placeholder="请输入方案标签" />
        </el-form-item>
        <el-form-item label="方案状态" prop="status">
          <el-select v-model="form2.status" placeholder="请选择方案状态" clearable>
            <el-option label="进行中" value="1"/>
            <el-option label="已完成" value="2"/>
            <el-option label="已失败" value="3"/>
          </el-select>
        </el-form-item>
        <el-form-item label="预估价格" prop="prePrice">
          <el-input v-model="form2.prePrice" placeholder="请输入预估价格" />
        </el-form-item>
        <el-form-item label="供方公司" prop="companyName">
          <el-input v-model="form2.companyName" placeholder="请输入方案提供方公司名称" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm2">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listRequire, getRequire, delRequire, addRequire, updateRequire, changeStatus } from "@/api/system/require";
import { addSummary, updateSummary } from "@/api/system/summary";
import { listUser } from "@/api/system/user";
import VueMarkdown from 'vue-markdown';

export default {
  name: "Require",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 用户需求信息表格数据
      requireList: [],
      // 用户列表选项
      userOptions: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 是否显示弹出层
      openSummary: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        userId: null,
        sessionId: null,
        topicId: null,
        title: null,
        content: null,
        tags:null,
      },
      // 表单参数
      form: {},
      form2: {},
      // 表单校验
      rules: {
          userId: [
              { required: true, message: "需求方用户不能为空", trigger: "blur" }
          ],
          sessionId: [
              { required: true, message: "会话标识不能为空", trigger: "blur" }
          ],
          topicId: [
              { required: true, message: "主题不能为空", trigger: "blur" }
          ],
          title: [
              { required: true, message: "需求标题不能为空", trigger: "blur" }
          ],
          content: [
              { required: true, message: "需求内容不能为空", trigger: "blur" }
          ],
          tags: [
              { required: true, message: "需求标签不能为空", trigger: "blur" }
          ],
          sort: [
              { required: true, message: "排序不能为空", trigger: "blur" }
          ],
      },
      rules2: {
          requireId: [
              { required: true, message: "需求不能为空", trigger: "blur" }
          ],
          userId: [
              { required: true, message: "供方用户不能为空", trigger: "change" }
          ],
          tag: [
              { required: true, message: "推荐标签不能为空", trigger: "blur" }
          ],
          mainImage: [
              { required: true, message: "方案主图不能为空", trigger: "change" }
          ],
          status: [
              { required: true, message: "方案状态不能为空", trigger: "change" }
          ],
          content: [
              { required: true, message: "方案内容不能为空", trigger: "blur" }
          ],
          prePrice: [
              { required: true, message: "预估价格不能为空", trigger: "blur" }
          ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询用户需求信息列表 */
    getList() {
      this.loading = true;
      listRequire(this.queryParams).then(response => {
        this.requireList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.openSummary = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        userId: null,
        identify: null,
        sessionId: null,
        topicId: null,
        title: null,
        content: null,
        tags: null,
        status: null,
        sort: null,
        createTime: null,
        updateTime: null
      };
      this.resetForm("form");
      this.form2 = {
        id: null,
        requireId: null,
        title: null,
        userId: null,
        tag: null,
        mainImage: null,
        content: null,
        prePrice: null,
        companyName: null,
        createTime: null,
        updateTime: null
      };
      this.resetForm("form2");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加商机信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getRequire(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.openSummary = false;
        this.title = "修改商机信息";
      });
    },
    /** 总结方案按钮操作 */
    handleSummary(row) {
      this.reset();
      const id = row.id || this.ids
      listUser(this.addDateRange(this.queryParams, this.dateRange)).then(response => {
          this.userOptions = response.rows;
        }
      );
      this.form2.requireId = id;
      this.form2.title = row.title;
      this.open = false;
      this.openSummary = true;
      this.title = "新增合作方案";
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateRequire(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addRequire(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    submitForm2() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form2.id != null) {
            updateSummary(this.form2).then(response => {
                this.$modal.msgSuccess("修改成功");
                this.openSummary = false;
            });
          } else {
            addSummary(this.form2).then(response => {
                this.$modal.msgSuccess("新增成功");
                this.openSummary = false;
            });
          }
        }
      });
    },
    // 状态修改
    handleStatusChange(row) {
        let text = row.status === "0" ? "启用" : "停用";
        this.$modal.confirm('确认要"' + text + '""' + row.title + '"商机吗？').then(function() {
            return changeStatus(row.id, row.status);
        }).then(() => {
            this.$modal.msgSuccess(text + "成功");
        }).catch(function() {
            row.status = row.status === "0" ? "1" : "0";
        });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除编号为"' + ids + '"的数据项？').then(function() {
        return delRequire(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/require/export', {
        ...this.queryParams
      }, `require_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
